
import * as types from '../types'


const state = {
  showSideBar: false,
  searchHistory: ['毛不易', '周杰伦', '陈奕迅', '小杜']
}

const mutations = {
  [types.COM_SHOW_SIDE_BAR](state, status) {
    state.showSideBar = status
  },
  setShow(state) {
    state.showSideBar = !state.showSideBar
  },
  [types.COM_SAVE_SEARCH_HISTORY](state, item) {
    if (!state.searchHistory.includes(item) && item) {
      console.log('开始添加数据', item);
      state.searchHistory.unshift(item)

    }
  },
  [types.COM_DELETE_SEARCH_HISTORY](state, item) {
    let index = state.searchHistory.indexOf(item)
    console.log('删除数据');
    let tmp = state.searchHistory
    state.searchHistory = [...tmp.slice(0, index), ...tmp.slice(index + 1)]
  },
  [types.COM_CLEAR_SEARCH_HISTORY](state) {
    state.searchHistory = []
  }
}

const actions = {
  setShowSideBar({ commit }, status) {
    commit(types.COM_SHOW_SIDE_BAR, status)
  },
  saveHistory({ commit }, history) {
    commit(types.COM_SAVE_SEARCH_HISTORY, history)
  },
  deleteHistory({ commit }, item) {
    commit(types.COM_DELETE_SEARCH_HISTORY, item)
  },
  clearHistory({ commit }) {
    commit(types.COM_CLEAR_SEARCH_HISTORY)
  }

}

const getters = {
  showSideBar: state => { return state.showSideBar },
  searchHistory: state => { return state.searchHistory }
}

export default {
  state,
  mutations,
  actions,
  getters
}